package com.bootdo.clouddo.controller.douban;


import com.bootdo.clouddo.annotation.Decrypt;
import com.bootdo.clouddo.domain.douban.DbBookDetail;
import com.bootdo.clouddo.service.douban.*;
import com.bootdo.clouddo.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book/douban")
public class BookController {

    @Autowired
    private IDbBookDetailService biz;

    @Autowired
    private IDbBookRatingService ratingService;

    @Autowired
    private IDbBookInterestService interestService;

    @Autowired
    private IDbBookReviewService reviewService;

    @Autowired
    private IDbBookReviewFullService reviewFullService;

    @Autowired
    private IDbSubjectCollectionService collectionService;
    @Autowired
    private  IDbSubjectCollectionItemService collectionItemService;

    @Autowired
    private IDbBookRecommendationService recommendationService;

    @Decrypt
    @RequestMapping("/detail")
    public R findById(@RequestBody DbBookDetail book){
        try{
            if(book.getId() != null && book.getId() > 0)
                return R.data(biz.findById(book.getId()));
            return R.data(biz.getRandom());
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/rating/{id}")
    public R findRatingById(@PathVariable Long id){
        try{
            return R.data(ratingService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/interest/{subjectId}")
    public R findInterestById(@PathVariable Long subjectId, @RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="4")Integer size){
        try{
            PageHelper.startPage(page, size);
            return R.data(new PageInfo<>(interestService.findBySubjectId(subjectId)));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/review/{subjectId}")
    public R findReviewById(@PathVariable Long subjectId, @RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="6")Integer size){
        try{
            PageHelper.startPage(page, size);
            return R.data(new PageInfo<>(reviewService.findBySubjectId(subjectId)));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/review/detail/{id}")
    public R findReviewDetailById(@PathVariable Long id){
        try{
            return R.data(reviewFullService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/recommend/{id}")
    public R findRecommend(@PathVariable Long id){
        try{
            return R.data(recommendationService.findBySubjectId(id));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/collect/{collectionId}")
    public R findCollection(@PathVariable String collectionId){
        try{
            return R.data(collectionService.findById(collectionId));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }


    @GetMapping("/collect/items/{collectionId}")
    public R findItemsByCollection(@PathVariable String collectionId, @RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="6")Integer size){
        try{
            PageHelper.startPage(page, size);
            return R.data(new PageInfo<>(collectionItemService.findItemsByCollection(collectionId)));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/find")
    public R find(String keyword, @RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer size){
        try{
            PageHelper.startPage(page, size);
            return R.data(new PageInfo<>(biz.find(keyword)));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }

}
